package com.zhongxi.guoyuan.widget;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.SeekBar;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class Player implements OnBufferingUpdateListener,  
OnCompletionListener, MediaPlayer.OnPreparedListener,  
SurfaceHolder.Callback {  
	private int videoWidth;  
	private int videoHeight;  
	public MediaPlayer mediaPlayer;  
	private SurfaceHolder surfaceHolder;  
	private SeekBar skbProgress;  
	private String url;
	private Timer mTimer=new Timer();  
	public Player(SurfaceView surfaceView,SeekBar skbProgress, String url)  
	{  
		this.skbProgress=skbProgress;  
		surfaceHolder=surfaceView.getHolder();  
		surfaceHolder.addCallback(this);  
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  
		mTimer.schedule(mTimerTask, 0, 1000);  
		this.url = url;
	}  

	/******************************************************* 
	 ******************************************************/
	TimerTask mTimerTask = new TimerTask() {  
		@Override  
		public void run() {  
			if(mediaPlayer==null)  
				return;  
			if (mediaPlayer.isPlaying() && skbProgress.isPressed() == false) {  
				handleProgress.sendEmptyMessage(0);  
			}  
		}  
	};  

	Handler handleProgress = new Handler() {  
		public void handleMessage(Message msg) {  

			int position = mediaPlayer.getCurrentPosition();  
			int duration = mediaPlayer.getDuration();  

			if (duration > 0) {  
				long pos = skbProgress.getMax() * position / duration;  
				skbProgress.setProgress((int) pos);  
			}  
		};  
	};  
	//*****************************************************  

	public void play()  
	{  
		mediaPlayer.start();  
	}  

	public void playUrl()  
	{  
		try {  
			mediaPlayer.reset(); 
			mediaPlayer.setDataSource(url);  
			mediaPlayer.prepare();//prepare֮
			//mediaPlayer.start();  
		} catch (IllegalArgumentException e) {  
			e.printStackTrace();  
		} catch (IllegalStateException e) {  
			e.printStackTrace();  
		} catch (IOException e) {   
			e.printStackTrace();  
		}  
	}  


	public void pause()  
	{  
		mediaPlayer.pause();  
	}  

	public void stop()  
	{  
		if (mediaPlayer != null) {   
			mediaPlayer.stop();  
			mediaPlayer.release();   
			mediaPlayer = null;   
		}   
	}  

	@Override  
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {  
		Log.e("mediaPlayer", "surface changed");  
	}  

	@Override  
	public void surfaceCreated(SurfaceHolder arg0) {  
		try {  
			mediaPlayer = new MediaPlayer();  
			mediaPlayer.setDisplay(surfaceHolder);  
			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
			mediaPlayer.setOnBufferingUpdateListener(this);  
			mediaPlayer.setOnPreparedListener(this);
			playUrl();
		} catch (Exception e) {  
			Log.e("mediaPlayer", "error", e);  
		}  
		Log.e("mediaPlayer", "surface created");  
	}  

	@Override  
	public void surfaceDestroyed(SurfaceHolder arg0) {  
		Log.e("mediaPlayer", "surface destroyed");  
	}  


	@Override  
	/**  
	 */
	public void onPrepared(MediaPlayer arg0) {  
		videoWidth = arg0.getVideoWidth();  
		videoHeight = arg0.getVideoHeight();  
		if (videoHeight != 0 && videoWidth != 0) {  
			arg0.start();  
		}  
		Log.e("mediaPlayer", "onPrepared");  
	}  

	@Override  
	public void onCompletion(MediaPlayer arg0) {  

	}  

	@Override  
	public void onBufferingUpdate(MediaPlayer arg0, int bufferingProgress) {  
		skbProgress.setSecondaryProgress(bufferingProgress);  
		int currentProgress=skbProgress.getMax()*mediaPlayer.getCurrentPosition()/mediaPlayer.getDuration();  
		Log.e(currentProgress+"% play", bufferingProgress + "% buffer");  

	}

	public void onDestroyed(){
		if(mediaPlayer!=null)
			mediaPlayer = null;
	}

}
